<?php

class NewsletterModel extends Newsletter{

	public function insertNewsletter($arr){
		try {
			$obj = new Newsletter();
			foreach($arr as $chave => $valor){
				if($chave != 'idnewsletter'){
					$chave = ucfirst($chave);
					$met = 'set'.$chave;
					$obj->$met($valor);
				}
			}
			$db = new Db;
			$db->Insert('newsletter',$obj);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function updateNewsletter($arr){
		try {
			$obj = new Newsletter();
			foreach($arr as $chave => $valor){
				$chave = ucfirst($chave);
				$met = 'set'.$chave;
				$obj->$met("'".$valor."'");
			}
			$db = new Db;
			$db->Update('newsletter',$obj);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function deleteNewsletter($id){
		try {
			$db = new Db;
			$db->Delete('newsletter',$id);
			$this->lista = $db->res;
		} catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function consultarNewsletter($parametros){
		try {
			$campos = array('idnewsletter','nome','email');
			$where = 'idnewsletter = '.$parametros;
			$order = '';
			$db = new Db;
			$db->Select('newsletter',$campos,$where,$order);
			$this->lista = $db->res[0];
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function listarNewsletter(){
		try {
			$campos = array('idnewsletter','nome','email');
			$where = '';
			$order = 'nome DESC';
			$db = new Db;
			$db->Select('newsletter',$campos,$where,$order);
			$view = new NewsletterView();
			$this->lista = $view->listar($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
}
?>